var selectalluserlik = webrootpath+"user/selectalluser";
var adduserlink = webrootpath+"user/adduser";
var deleteuserlink = webrootpath+"user/deleteuserlink";

var getuserinfolink = webrootpath+"user/getuserinfolink";
var initcatedatabyuserlink = webrootpath+"user/initcatedatabyuserlink";  //加载分类树
var initcatedatabyuser_lm_link = webrootpath+"user/initcatedatabyuser_lm_link";  //加载分类树(栏目分类)

var index_user_auth_modal_addauthlink = webrootpath+"user/index_user_auth_modal_addauthlink";//保存用户权限（ner分类权限）
var index_user_auth_modal_lm_addauthlink = webrootpath+"user/index_user_auth_modal_lm_addauthlink";//保存用户权限（栏目权限）

var chec_lmdoclink = webrootpath+"user/chec_lmdoclink"; //增加用户栏目文档编辑权限
var finduserlmdocauthlink = webrootpath+"user/finduserlmdocauthlink"; //查询用户文档权限位

var edit_user_pwdlink = webrootpath+"user/edit_user_pwdlink"; //修改密码

var selectedituserid = 0;//编辑权限选中的用户id
var selecteditlmid = 0;//编辑栏目文档权限选中的栏目id
$(function () {
    //判断用户状态
    booluserloginstatus();

});

/****
 * 导航
 * @param _msg
 */
function index_user_auth_modal_lm_menu_tab(_msg) {
    $("#index_user_auth_modal_lm_menu").find("li").removeClass("active");
    $("#index_user_auth_modal_lm_menu_content_0").hide();
    $("#index_user_auth_modal_lm_menu_content_1").hide();
    if(_msg=="0"){
        $("#index_user_auth_modal_lm_menu").find("li:eq(0)").addClass("active");
        $("#index_user_auth_modal_lm_menu_content_0").show();
    }else if(_msg=="1"){
        $("#index_user_auth_modal_lm_menu").find("li:eq(1)").addClass("active");
        $("#index_user_auth_modal_lm_menu_content_1").show();
    }
}

/***
 * 添加编辑属性
 * @param _this
 */
function chec_lmdoc(_this) {
    var tval = $(_this).val();
    var ischecked = $(_this).prop("checked");
    $.get(chec_lmdoclink,{
        selecteditlmid:selecteditlmid,
        tval:tval,
        ischecked:ischecked,
        selectedituserid:selectedituserid
    },function (_info) {
        if(_info.code=="1"){
            $.alert(_info.msg);
            return false;
        }
    });
}
/***
 * 添加用户栏目权限及操作权限
 */
function index_user_auth_modal_lm_addauth() {

    var selecttreeobj = $.fn.zTree.getZTreeObj("index_user_auth_modal_lm_tree");
    var nodes = selecttreeobj.getCheckedNodes(true);
    var nodeidstr = "";
    for(var i in nodes){
        var nodesobj = nodes[i];
        nodeidstr+=","+nodesobj.cateid;
    }
    if(nodeidstr==""){
        alert("请选择访问权限");
        return false;
    }

    $.post(index_user_auth_modal_lm_addauthlink,{
        selectedituserid:selectedituserid,
        nodeidstr:nodeidstr
    },function (_info) {
        $("#index_user_auth_modal_lm").modal("hide");
        if(_info.code=="1"){
            alert("保存成功");
        }
    });

}
/****
 * 编辑栏目权限(弹出框)
 * @param _userid
 */
function edit_user_auth_modal_lm(_userid) {
    selectedituserid = _userid;
    $("#index_user_auth_modal_lm").modal("show");
    edit_user_auth_modal_lm_tree(-1);
    finduserlmdocauth(0);
}
function edit_user_auth_modal_lm_tree(_lmid) {

    $.get(initcatedatabyuser_lm_link,{
        node:_lmid,
        selectedituserid:selectedituserid
    },function (data) {
        if (data.code=='1'){

            var zTreeObj;
            // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
            var setting = {
                data:{
                    keep:{
                        parent:true
                    }
                },
                view: {
                    dblClickExpand: true,
                    showLine: true
                },
                check: {
                    enable: true ,//显示复选框
                    chkStyle : "checkbox"
                },
                callback:{
                    onRightClick:function (event, treeId, treeNode) {

                    },
                    onExpand:function (event, treeId, treeNode) {

                        var treenodeid = treeNode.cateid;
                        $.get(initcatedatabyuser_lm_link,{
                            node:treenodeid,
                            selectedituserid:selectedituserid
                        },function (data) {
                            if (data.code=='1'){
                                var sonnode = data.data.datalist;
                                zTreeObj.addNodes(treeNode, sonnode);
                            }
                        });
                    },
                    onCollapse:function (event, treeId, treeNode) {
                        var treenodeid = treeNode.cateid;
                        zTreeObj.removeChildNodes(treeNode);
                    },
                    onClick:function (event, treeId, treeNode,clickFlag) {
                        selecteditlmid = treeNode.cateid;
                        finduserlmdocauth(selecteditlmid);
                    }
                }
            };

            // zTree 的数据属性，深入使用请参考 API 文档（zTreeNode 节点数据详解）
            zTreeObj = $.fn.zTree.init($("#index_user_auth_modal_lm_tree"), setting, data.data.datalist);
            var nodes = zTreeObj.getNodes();
            zTreeObj.selectNode(nodes[0]);
        }
    });

}

/***
 * 查询用户文档权限位
 * @param _lmid
 */
function finduserlmdocauth(_lmid) {
    $.get(finduserlmdocauthlink,{
        lmid:_lmid,
        selectedituserid:selectedituserid
    },function (_info) {
        $("#index_user_auth_modal_lm_menu_content_1").find("input[type='checkbox']").prop("checked",false);
        if(_info.code=="1"){
            var datalist = _info.data;
            for(var i in datalist){
                var dataobj = datalist[i];
                var optype = dataobj.optype;
                $("#index_user_auth_modal_lm_menu_content_1").find("input[type='checkbox']").each(function (_index,_this) {
                    var checkval  = $(_this).val();
                    if(checkval==optype){
                        $(_this).prop("checked",true);
                    }
                });
            }
        }else {
            $("#index_user_auth_modal_lm_menu_content_1").find("input[type='checkbox']").prop("checked",false);

        }
    });
}


/***
 * 注销登录
 */
function userBack() {
    sessionStorage.removeItem("usertoken");
    window.location.href = "./login.html";
}
/***
 * 用户登录状态
 * @returns {boolean}
 */
function booluserloginstatus() {
    var usertoken = sessionStorage.getItem("usertoken");
    if(usertoken!=null && usertoken!=undefined){
        $.get(getuserinfolink,{usertoken:usertoken},function (_info) {
            if(_info.code=="1"){
                var userinfo = _info.data;
                $("#index_div_1_loginuser").html(userinfo.username);
                var datalist = _info.datalist;
                console.log(datalist);
                var cdmenuhtmlstr = "";
                for(var i in datalist){
                    var dataobj = datalist[i];
                    cdmenuhtmlstr+="<li id=\""+dataobj.tagidstr+"\">"+dataobj.tagname+"</li>";

                }
                $("#index_div_2_1_ul").html(cdmenuhtmlstr);
                indexUl();
            }else {
                $.confirm({
                    title: '提示',
                    content: '用户登录超时，请重新登录',
                    buttons: {
                        确定: function () {
                            window.location.href = "./login.html";
                        }
                    }
                });
                return false;
            }
        });
    }else {
        $.confirm({
            title: '提示',
            content: '用户登录超时，请重新登录',
            buttons: {
                确定: function () {
                    window.location.href = "./login.html";
                }
            }
        });
        return false;
    }
}

/***
 * 用户导航
 */
function indexUl() {
    $("#index_div_2_1_ul").find("li").each(function(_index,_this){
        $(_this).click(function(){
            var sv = $(_this).attr("id");
            $("#index_div_2_1_ul li").css("background-color","#f8f8f8");
            $("#index_div_2_1_ul li").css("font-weight","normal");
            $("#index_div_2_1_ul li").css("color","black");
            $(_this).css("background-color","#84C1FF");
            $(_this).css("font-weight","bold");
            $(_this).css("color","#FFFFFF");
            if(sv=='index_div_2_1_ul_yhgl'){
                //用户管理
                $("#index_div_2_2_id").load("./index_user.html");
                selectalluser(1);
            }else if(sv=='index_div_2_1_ul_shenqingdan'){
                //申请单配置
                $("#index_div_2_2_id").load("./index_shenqingdan.html");
            }else if ('index_div_2_1_ul_lmgl'==sv){
                //栏目管理
                $("#index_div_2_2_id").load("./index_lm.html");
            }else if(sv=='index_div_2_1_ul_gzkgl'){
                //规则库管理
                $("#index_div_2_2_id").load("./index_gzkgl.html?x="+Math.random());
            }else if(sv=='index_div_2_1_ul_cbgl'){
                //词表管理
                $("#index_div_2_2_id").load("./word/index_word.html?x="+Math.random());
            }else if (sv=='index_div_2_1_ul_nlp_ner'){
                //nlp命名实体数据标记
                $("#index_div_2_2_id").load("./nlp_ner/index_nlp_ner.html?x="+Math.random());
            }else if(sv=='index_div_2_1_ul_gjzx'){
                //工具中心
                $("#index_div_2_2_id").load("./tools/index_tools.html?x="+Math.random());
            }else if (sv=='index_div_2_1_ul_rzgl'){
                //日志管理
                $("#index_div_2_2_id").load("./logs/index_logs.html?x="+Math.random());
            }
        });
    });
    $("#index_div_2_1_ul").find("li:eq(0)").click();
}
function index_user_tab(_msg) {
    $("#index_user_ul_tab").find("li").removeClass("active");
    $("#index_user_ul_tab").find("li").each(function (_index,_this) {
        if(_index==_msg){
            $(_this).addClass("active");
        }
    });
    if(_msg==0){
        //用户管理
        $("#index_div_2_2_id").load("./index_user.html");
        selectalluser(1);
    }else if (_msg==1){
        //组织管理
        $("#index_div_2_2_1").load("./index_user_group.html");


    }else if (_msg==2){
        //数据初始化
        $("#index_div_2_2_1").load("./index_data_chushihua.html");


    }
}



//增加用户弹出框
function index_div_2_2_1_btnadd_view() {

        $("#adduser_form").find("input[type='text']").val("");
        $("#adduser_dialog").modal('show');
        $("#adduser_dialog_submit").click(function () {
            var formdata = $("#adduser_form").serialize();
            $.ajax({
                type:"post",
                url:adduserlink,
                data:formdata,
                async:true,
                dataType:"json",
                cache:false,
                success:function(data){
                    $("#adduser_dialog").modal('hide');
                    if(data.code=="1"){
                        selectalluser(1);
                        alert(data.msg);
                    }else{
                        alert(data.msg);
                    }
                },
                error:function(){

                    $("#adduser_dialog").modal('hide');
                    alert("添加失败");
                }

            });


        });
}




/***
 * 删除用户
 */
function index_div_2_2_1_btndelete_view() {
        var checkval = "";
        $("input[name='userinfo_id']:checked").each(function (_index,_this) {
            checkval+=","+$(_this).val();
        });
        if(checkval!=""){
            checkval = checkval.substring(1);
            var r = window.confirm("您确定要删除这些用户吗？");
            if(r){
                $.post(deleteuserlink,{checkval:checkval},function (data) {
                    alert(data.msg);
                    selectalluser(1);
                });
            }
        }else {
            alert("请选择要删除的用户");
        }
}

/***
 * 修改密码
 */
function edit_user_pwd_init() {
    var userpwd = $("#edit_user_pwd_modal_userpwd").val();
    var userpwd2 = $("#edit_user_pwd_modal_userpwd2").val();
    if(userpwd=="" || userpwd2==""){
        alert("密码不能为空");
        return false;
    }
    if(userpwd!=userpwd2){
        alert("两次密码结果不相同");
        return false;
    }
    var edit_user_pwd_userid = sessionStorage.getItem("edit_user_pwd_userid");
    if(edit_user_pwd_userid!=null && edit_user_pwd_userid!=undefined && edit_user_pwd_userid!="" && edit_user_pwd_userid!="undefined"){
        $.get(edit_user_pwdlink,{
            userid:edit_user_pwd_userid,
            userpwd:userpwd
        },function (_info) {
            $("#edit_user_pwd_modal").modal("hide");
            if(_info.code=="1"){
                alert(_info.msg);

            }
        });
    }



}

/***
 * 修改密码_弹出框
 * @param _userid
 */
function edit_user_pwd(_userid,_username) {
    sessionStorage.setItem("edit_user_pwd_userid",_userid);
    $("#edit_user_pwd_modal").modal("show");
    $("#edit_user_pwd_modal_userpwd").val("");
    $("#edit_user_pwd_modal_userpwd2").val("");
    $("#edit_user_pwd_modal_updateusernmae").html(_username);

}



//查询所有用户
function selectalluser(pagenum){
    $.ajax({
        type:"post",
        url:selectalluserlik,
        data:{
            pagenum:pagenum
        },
        async:true,
        cache:false,
        dataType:"json",
        success:function(content){
            console.log(content);
            if(content.code=="1"){
                var datalist = content.data.datalist;
                var pagetotal = content.data.pagetotal;
                var pagesize = content.data.pagesize;
                var pagenum = content.data.pagenum;
                userlistpage(pagenum,pagetotal);

                var htmlstr = "";
                for(var i in datalist){
                    var contentobj = datalist[i];
                    htmlstr += "<tr>" +
                                    "<td><input name='userinfo_id' value='"+contentobj.userid+"' type='checkbox'>"+contentobj.userid+"</td>" +
                                    "<td>"+contentobj.username+"</td>" +
                                    "<td>"+contentobj.userstatus+"</td>" +
                                    "<td>"+contentobj.usertruename+"</td>" +
                                    "<td>"+contentobj.usercompany+"</td>" +
                                    "<td>"+contentobj.userphone+"</td>" +
                                    "<td>"+contentobj.useremail+"</td>" +
                                    "<td>"+timestampToTime(contentobj.userdate)+"</td>" +
                                    "<td>" +
                                        "<button type='button' class='btn btn-info'   onclick='edit_user_auth_modal_lm("+contentobj.userid+")' style='margin-left: 20px;'  >编辑栏目权限</button>" +
                                        "<button type='button' class='btn btn-info'   onclick='edit_user_auth_modal("+contentobj.userid+")' style='margin-left: 20px;'  >编辑NER标记权限</button>" +
                                        "&nbsp;&nbsp;<button type=\"button\" class=\"btn btn-info\" onclick='edit_user_pwd("+contentobj.userid+",\""+contentobj.username+"\")'>修改密码</button>"+
                                    "</td>" +
                                "</tr>";
                }
                $("#index_div_2_2_1_userlist").html(htmlstr);

            }

        }

    });
}
//数据分页
function userlistpage(_pagenum,_pagezong){
    //处理分页
    $('#index_div_2_2_1_userlist_page').bootstrapPaginator({
        currentPage: _pagenum,
        totalPages: _pagezong,
        size:"normal",
        bootstrapMajorVersion: 3,
        alignment:"right",
        numberOfPages:5,
        itemTexts: function (type, page, current) {
            switch (type) {
                case "first":
                    return "首页";
                case "prev":
                    return "上一页";
                case "next":
                    return "下一页";
                case "last":
                    return "末页";
                case "page":
                    return page;
            }
        },
        onPageClicked: function (event, originalEvent, type, page) {
            //alert(page);
            selectalluser(page);
        }
    });
}


/***
 * 获取词表分类数据
 */
function findlmdata_link_fun_2(_node) {
    $.get(initcatedatabyuserlink,{node:_node,selectedituserid:selectedituserid},function (data) {
        if (data.code=='1'){
            initindex_user_auth_modal_tree(data.data);
        }
    });
}
/***
 * 初始化树结构
 */
function initindex_user_auth_modal_tree(_treedata) {
    var zTreeObj;
    // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
    var setting = {
        data:{
            keep:{
                parent:true
            }
        },
        view: {
            dblClickExpand: true,
            showLine: true
        },
        check: {
            enable: true ,//显示复选框
            chkStyle : "checkbox"
        },
        callback:{
            onExpand:function (event, treeId, treeNode) { 
                var zAsync = treeNode.zAsync;
                //展开
                var treenodeid = treeNode.cateid;
                $.get(initcatedatabyuserlink,{node:treenodeid,selectedituserid:selectedituserid},function (data) {
                    if (data.code=='1'){
                        var sonnode = data.data.datalist;
                        zTreeObj.addNodes(treeNode, sonnode);
                    }
                });
            },
            onCollapse:function (event, treeId, treeNode) {
                //折叠后
                var treenodeid = treeNode.cateid;
                zTreeObj.removeChildNodes(treeNode);
            },
            onClick:function (event, treeId, treeNode,clickFlag) {
            },
            onCheck:function (event, treeId, treeNode) {


            }
        }
    };
    // zTree 的数据属性，深入使用请参考 API 文档（zTreeNode 节点数据详解）
    zTreeObj = $.fn.zTree.init($("#index_user_auth_modal_tree"), setting, _treedata.datalist);
    var nodes = zTreeObj.getNodes();
    zTreeObj.selectNode(nodes[0]);
}
/****
 * 编辑用户权限弹出框
 */
function edit_user_auth_modal(_userid) {
    selectedituserid = _userid;
    $("#index_user_auth_modal").modal("show");

    findlmdata_link_fun_2(-1);
}
/***
 * 保存权限
 */
function index_user_auth_modal_addauth() {

    var selecttreeobj = $.fn.zTree.getZTreeObj("index_user_auth_modal_tree");
    var nodes = selecttreeobj.getCheckedNodes(true);
    var nodeidstr = "";
    for(var i in nodes){
        var nodesobj = nodes[i];
        nodeidstr+=","+nodesobj.cateid;
    }
    if(nodeidstr==""){
        alert("请选择访问权限");
        return false;
    }

    $.post(index_user_auth_modal_addauthlink,{
        selectedituserid:selectedituserid,
        nodeidstr:nodeidstr
    },function (_info) {
        $("#index_user_auth_modal").modal("hide");
        if(_info.code=="1"){
            alert("保存成功");
        }
    });
}